package com.maple.retry.retrytask.schedule;

import com.maple.retry.retrytask.manager.RetryTaskManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: maple
 * @version: RetryTaskSchedule.java, v 0.1 2020年12月28日 17:29 maple Exp $
 */
public class RetryTaskSchedule {

    private Map<String, List<String>> bizTypeMap = new ConcurrentHashMap<>();

    @Autowired
    private RetryTaskManager retryTaskManager;

    @Scheduled(cron = "*/5 * * * * ?")
    private void configureTasks() {

        List<String> bizTypes = bizTypeMap.get("RETRY_TASK_KEY");

        for (String bizType : bizTypes) {
            retryTaskManager.process(bizType);
        }
    }

    public void setBizTypeMap(Set<Map<String, String>> eventCodeMaps) {
        for (Map<String, String> eventCodeMap : eventCodeMaps) {
            for (Map.Entry<String, String> entry : eventCodeMap.entrySet()) {
                if (this.bizTypeMap.containsKey(entry.getKey())) {
                    this.bizTypeMap.get(entry.getKey()).add(entry.getValue());
                } else {
                    List<String> bizTypes = new ArrayList<>();
                    bizTypes.add(entry.getValue());
                    this.bizTypeMap.put(entry.getKey(), bizTypes);
                }
            }
        }
    }
}



